﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> mergePdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create the merge result PDF document</span>
    Document mergeResultPdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Automatically close the merged documents when the document resulted after merge is closed</span>
    mergeResultPdfDocument<span style='color:#808030; '>.</span>AutoCloseAppendedDocs <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>true</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    mergeResultPdfDocument<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// The documents to merge must remain opened until the PDF document resulted after merge is saved</span>
        <span style='color:#696969; '>// The merged documents can be automatically closed when the document resulted after merge is closed</span>
        <span style='color:#696969; '>// if the AutoCloseAppendedDocs property of the PDF document resulted after merge is set on true like</span>
        <span style='color:#696969; '>// in this demo applcation</span>

        <span style='color:#696969; '>// Load the first PDF document to merge</span>
        <span style='color:#800000; font-weight:bold; '>string</span> firstPdfFilePath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        Document firstPdfDocumentToMerge <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span>firstPdfFilePath<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#696969; '>// Merge the first PDF document</span>
        mergeResultPdfDocument<span style='color:#808030; '>.</span>AppendDocument<span style='color:#808030; '>(</span>firstPdfDocumentToMerge<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Load the second PDF document to merge</span>
        <span style='color:#800000; font-weight:bold; '>string</span> secondPdfFilePath <span style='color:#808030; '>=</span> Server<span style='color:#808030; '>.</span>MapPath<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>~/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        Document secondPdfDocumentToMerge <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span>secondPdfFilePath<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#696969; '>// Merge the second PDF document</span>
        mergeResultPdfDocument<span style='color:#808030; '>.</span>AppendDocument<span style='color:#808030; '>(</span>secondPdfDocumentToMerge<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Save the merge result PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> mergeResultPdfDocument<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Send the PDF as response to browser</span>

        <span style='color:#696969; '>// Set response content type</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=Merge_PDF.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
        Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
        Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>finally</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Close the PDF document resulted after merge</span>
        <span style='color:#696969; '>// When the AutoCloseAppendedDocs property is true the merged PDF documents will also be closed</span>
        mergeResultPdfDocument<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</span>